package com.uwlaser.modules.system.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.modules.system.service.SysLogsService;
import com.uwlaser.modules.system.service.dto.SysLogsDto;
import com.uwlaser.modules.system.service.dto.SysLogsQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
* @author liuzhihui
* @date 2022-03-27
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "接口日志管理")
@RequestMapping("/api/sysLogs")
public class SysLogsController {

    private final SysLogsService sysLogsService;

    @GetMapping
    @Log("查询接口日志")
    @ApiOperation("查询接口日志")
    @PreAuthorize("@el.check('sysLogs:list')")
    public ResponseEntity query(SysLogsQueryParam query, Pageable pageable){
        return new ResponseEntity<>(sysLogsService.queryAll(query,pageable),HttpStatus.OK);
    }

    @GetMapping("one")
    @Log("查询接口日志详情")
    @ApiOperation("查询接口日志详情")
    @PreAuthorize("@el.check('sysLogs:list')")
    public ResponseEntity query(Integer id){
        return new ResponseEntity<>(sysLogsService.getById(id),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增接口日志")
    @ApiOperation("新增接口日志")
    @PreAuthorize("@el.check('sysLogs:add')")
    public ResponseEntity create(@Validated @RequestBody SysLogsDto resources){
        return new ResponseEntity<>(sysLogsService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改接口日志")
    @ApiOperation("修改接口日志")
    @PreAuthorize("@el.check('sysLogs:edit')")
    public ResponseEntity update(@Validated @RequestBody SysLogsDto resources){
        sysLogsService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除接口日志")
    @ApiOperation("删除接口日志")
    @PreAuthorize("@el.check('sysLogs:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        sysLogsService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出接口日志")
    @ApiOperation("导出接口日志")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('sysLogs:list')")
    public void download(HttpServletResponse response, SysLogsQueryParam query) throws IOException {
        sysLogsService.download(sysLogsService.queryAll(query), response);
    }*/

}
